package com.wlb365.financing.modules.esign.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.common.utils.ShiroUtil;
import com.wlb365.financing.common.utils.StringUtils;
import com.wlb365.financing.modules.esign.request.CreateOrgDto;
import com.wlb365.financing.modules.esign.request.CreatePersonDto;
import com.wlb365.financing.modules.esign.request.CreateSignFlowDto;
import com.wlb365.financing.modules.esign.service.ESignService;
import com.wlb365.financing.modules.sys.entity.Dept;
import com.wlb365.financing.modules.sys.entity.User;
import com.wlb365.financing.modules.sys.service.IDeptService;
import com.wlb365.financing.modules.sys.service.IUserService;
import com.wlb365.financing.modules.sys.vo.UserQueryVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 概述：电子签章
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2021/4/20
 */
@RestController
@RequestMapping("esign")
public class EsignController {
    @Autowired
    private ESignService eSignService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;

    //-----------个人签章账号 开始------------------

    @RequiresPermissions("esign:person:index")
    @GetMapping(value = "person/index")
    public ModelAndView person() {
        return new ModelAndView("esign/account/person");
    }

    @GetMapping(value = "person/list/data")
    public R personListData(UserQueryVO queryVO) {
        Page<User> page = userService.listUserPage(queryVO);
        return R.ok(page);
    }

    @RequiresPermissions("esign:person:create")
    @GetMapping(value = "person/create/view")
    public ModelAndView createPersonView(String id) {
        ModelAndView mv = new ModelAndView("esign/account/create_person");
        mv.addObject("editInfo", userService.getById(id));

        return mv;
    }

    @RequiresPermissions("esign:person:create")
    @PostMapping(value = "person/create")
    public R createPerson(CreatePersonDto dto) {

        return R.ok(eSignService.createPersonAcct(dto));
    }

    @RequiresPermissions("esign:person:cancel")
    @PostMapping(value = "person/cancel")
    public R cancelPerson(CreatePersonDto dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new AdminException("请选择要注销的签章账号");
        }
        eSignService.cancelPersonAcct(dto.getId());
        return R.ok();
    }

    @RequiresPermissions("esign:person:index")
    @GetMapping(value = "person/info")
    public ModelAndView personInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AdminException("请选择要查看的签章账号");
        }
        ModelAndView mv = new ModelAndView("esign/account/person_info");
        mv.addObject("editInfo", eSignService.queryPersonAcct(id));
        return mv;
    }

    //-----------个人签章账号 结束------------------

    //-----------机构签章账号 开始------------------

    @RequiresPermissions("esign:org:index")
    @GetMapping(value = "org/index")
    public ModelAndView org() {
        return new ModelAndView("esign/account/org");
    }

    @GetMapping(value = "org/list/data")
    public R orgListData(Dept dept) {
        User user = ShiroUtil.getUser();
        if(!user.getUsername().equals("admin")){
            dept.setId(user.getDeptId());
        }
        Page<Dept> page = deptService.listDeptPage(dept);
        return R.ok(page);
    }

    @RequiresPermissions("esign:org:create")
    @GetMapping(value = "org/create/view")
    public ModelAndView createOrgView(String id) {
        ModelAndView mv = new ModelAndView("esign/account/create_org");
        mv.addObject("editInfo", deptService.getById(id));

        return mv;
    }

    @RequiresPermissions("esign:org:create")
    @PostMapping(value = "org/create")
    public R createOrg(CreateOrgDto dto) {
        return R.ok(eSignService.createOrgAcct(dto));
    }

    @RequiresPermissions("esign:org:cancel")
    @PostMapping(value = "org/cancel")
    public R cancelOrg(CreateOrgDto dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new AdminException("请选择要注销的签章账号");
        }
        eSignService.cancelOrgAcct(dto.getId());
        return R.ok();
    }

    @RequiresPermissions("esign:org:index")
    @GetMapping(value = "org/info")
    public ModelAndView orgInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AdminException("请选择要查看的签章账号");
        }
        ModelAndView mv = new ModelAndView("esign/account/org_info");
        mv.addObject("editInfo", eSignService.querOrgAcct(id));
        return mv;
    }

    //-----------机构签章账号 结束------------------

    /**
    * 创建签章流程.
    * @param id 凭证id
    * @author zwh
    * @return com.wlb365.financing.common.dto.R
    **/
    @PostMapping(value = "createSignFlow")
    public R createSign(String id) {
        return R.ok(eSignService.createSignFlow(id));
    }

}
